// DirectXCore.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <DirectXException.h>
#include <Direct3DResource.h>
#include <D2DRenderer.h>

using namespace DirectXCore;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);

int main()
{
	auto resource = Direct3DResource::GetInstance();
	MSG msg;
	MyRegisterClass(::GetModuleHandleA(nullptr));
	InitInstance(::GetModuleHandleA(nullptr), SW_SHOW);

	// 主消息循环:
	while (GetMessage(&msg, nullptr, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int)msg.wParam;
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd = CreateWindowEx(
		//WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE,
		0,
		L"MyWindowClass", L"TestWindow",
		//WS_POPUP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 
		WS_OVERLAPPEDWINDOW,
		100, 100, 500, 500, nullptr, nullptr, hInstance, nullptr);

	if (!hWnd)
	{
		auto error = ::GetLastError();
		return FALSE;
	}

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);


	return TRUE;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEXW wcex = {};
	wcex.cbSize = sizeof(WNDCLASSEXW);

	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = WndProc;
	wcex.hInstance = hInstance;
	wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
	if (!wcex.hCursor) {
		// 错误处理
		return 0;
	}
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcex.lpszClassName = L"MyWindowClass";
	wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
	wcex.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);

	ATOM atom = RegisterClassExW(&wcex);
	if (!atom) {
		DWORD error = GetLastError();
		// 可以记录错误代码
		return 0;
	}
	return atom;
}
D2DRenderer* render;


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_CREATE:
	{
		render  = new D2DRenderer(hWnd);
	}
		break;
	case WM_SIZE:
	{
		render->Resize();
		render->Render();
	}
		break;
	case WM_PAINT:
	{
		break;
	}
	case WM_DESTROY:
	{
		delete render;
		PostQuitMessage(0);
		break;
	}
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}

	return 0;
}